import datetime
import time

from django.shortcuts import render, redirect

from drp import forms, models


# Create your views here.
def index(request):
    return render(request, 'drp/index.html')


# 药品入库
def Stock_View(request):
    Stock = forms.Stock_Form()
    if request.method == "POST":
        Stock = forms.Stock_Form(request.POST)
        if Stock.is_valid():
            factory = Stock.cleaned_data.get('factory')
            specification = Stock.cleaned_data.get('specification')
            name = Stock.cleaned_data.get('name')
            serial_number = time.strftime('%Y%m%d%H%M%S')
            num = Stock.cleaned_data.get('num')
            money = Stock.cleaned_data.get('money')
            money_all = str(num * money)
            models.Stock.objects.create(factory=factory, specification=specification, name=name, la_num=num,
                                        serial_number=serial_number, num=num, money=money, money_all=money_all)
            return render(request, 'drp/Stock_View.html', {'Stock': Stock, "msg": "添加成功"})
    return render(request, 'drp/Stock_View.html', {'Stock': Stock})


# 入库查询
def Stock_Select(request):
    stock_select = models.Stock.objects.all()
    if request.method == "POST":
        # 给筛选用的
        pass
    return render(request, 'drp/Stock_Select.html', locals())


# 药品出库
def Shipment_View(request):
    if request.method == "GET":
        # 接受过来的参数,赋值到表单中
        s_id = request.GET.get('sss')
        Shipment1 = models.Stock.objects.filter(id=s_id)
        data = {'factory': Shipment1[0].factory, 'specification': Shipment1[0].specification,
                'name': Shipment1[0].name, 'num': 0, 'money': 0}
        Shipment = forms.Shipment_Form(data)
        return render(request, 'drp/Shipment_View.html', locals())
    elif request.method == "POST":
        Shipment = forms.Shipment_Form(request.POST)
        if Shipment.is_valid():
            s_id = request.GET.get('sss')
            factory = Shipment.cleaned_data.get('factory')
            specification = Shipment.cleaned_data.get('specification')
            name = Shipment.cleaned_data.get('name')
            serial_number = time.strftime('%Y%m%d%H%M%S')
            num = Shipment.cleaned_data.get('num')
            money = Shipment.cleaned_data.get('money')
            money_all = str(num * money)
            stock = models.Stock.objects.filter(factory=factory, specification=specification, name=name)
            # 查询入库的数量和单价
            stock_num = stock[0].la_num
            stock_money = stock[0].money
            if num > stock_num:
                msg =  '库存数量不足'
                return render(request, 'drp/Shipment_View.html', locals())
            la_num = (stock_num - num)
            # 更新出库数据库
            models.Shipment.objects.create(factory=factory, specification=specification, name=name,
                                           serial_number=serial_number,
                                           num=num, money=money, money_all=money_all)
            stock.update(la_num=la_num)
            # 计算单品销售的利润 * 数量
            profit_money = (float(money) - float(stock_money))*num
            # 今天
            today = datetime.datetime.today()
            profit_obj = models.Profit.objects.filter(factory=factory, specification=specification,
                                                      name=name, date=today)
            if profit_obj.exists():
                profit_money_all = profit_obj[0].money_all
                profit_money_all = str(float(profit_money_all) + float(profit_money))
                profit_obj.update(money_all=profit_money_all)
            else:
                models.Profit.objects.create(factory=factory, specification=specification, name=name,
                                             money_all=profit_money)
            return redirect('Shipment_Select')


# 出库信息查询
def Shipment_Select(request):
    shipment_select = models.Shipment.objects.all()
    # 筛选用的
    if request.method == "POST":
        pass
    return render(request, 'drp/Shipment_Select.html', locals())


# 库存信息查询
def Stock_Shipment_Select(request):
    # 显示库存数量大于0的
    S_S_S = models.Stock.objects.filter(la_num__gt=0)
    # 筛选
    if request.method == "POST":
        pass
    return render(request, 'drp/Stock_Shipment_Select.html', locals())


# 利润查询
def Profit(request):
    profit = models.Profit.objects.all()
    if request.method == "POST":
        pass
    return render(request, 'drp/profit.html', locals())
